package il.technion.cs236369.webserver.typeHandlers;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import il.technion.cs236369.exceptions.BaseDirDoesntExist;
import il.technion.cs236369.exceptions.WelcomeFileDoesntExist;
import il.technion.cs236369.webserver.Request;
import il.technion.cs236369.webserver.WebServerConfiguration;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import junit.framework.Assert;

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicHttpRequest;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TSPEngineTests {

	private TSPEngine	engine;
	private String		filePath;
	private Request		request;

	@BeforeClass
	public static void beforeClass() throws BaseDirDoesntExist, WelcomeFileDoesntExist {
		WebServerConfiguration.init(8080, "C:/webserver", "index.html");
		Map<String, String> mimes = new HashMap<String, String>();
		mimes.put("html", "text/html");
		WebServerConfiguration.getInstance().setAllMimes(mimes);
	}

	@Before
	public void before() {
		Properties props = new Properties();
		props.setProperty("jre_path", "C:/Program Files/Java/jdk1.7.0_02");
		engine = new TSPEngine(props);
		request = new Request() {
			HttpRequest	request;
			{
				request = new BasicHttpRequest("GET", "URI");
				request.addHeader("Cookie", "TSPCookie=5");
			}

			@Override
			public String getAbsolutePath() {
				return filePath;
			}

			@Override
			public List<NameValuePair> getQueryParams() {
				return new LinkedList<NameValuePair>();
			}

			@Override
			public HttpRequest getRequest() {
				return request;
			}
		};
	}

	@Test
	public void testFileNotFound() {
		filePath = "foobar";
		HttpResponse result = engine.handle(request);
		assertEquals(404, result.getStatusLine().getStatusCode());
	}

	@Test
	public void testBadFile() {
		filePath = "TestFiles/test.xml";
		HttpResponse result = engine.handle(request);
		assertEquals(500, result.getStatusLine().getStatusCode());
	}

	@Test
	public void testMissingCookie() {
		filePath = "TestFiles/example1.tsp";
		request.getRequest().removeHeaders("Cookie");
		Assert.assertNull(request.getRequest().getFirstHeader("Cookie"));
		HttpResponse result = engine.handle(request);
		assertTrue(result.containsHeader("Set-Cookie"));
		assertEquals(200, result.getStatusLine().getStatusCode());
	}

	@Test
	public void testBadCookie() {
		filePath = "TestFiles/example1.tsp";
		HttpResponse result = engine.handle(request);
		assertEquals(400, result.getStatusLine().getStatusCode());
	}

	@Test
	public void testMissingCookieOk() {
		filePath = "TestFiles/tspTest.tsp";
		request.getRequest().removeHeaders("Cookie");
		Assert.assertNull(request.getRequest().getFirstHeader("Cookie"));
		HttpResponse result = engine.handle(request);
		assertTrue(result.containsHeader("Set-Cookie") == false);
		assertEquals(200, result.getStatusLine().getStatusCode());
	}
}
